package com.hiof.servercommunication.receive;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 3/25/12
 * Time: 3:14 PM
 * To change this template use File | Settings | File Templates.
 */

import com.hiof.servercommunication.receive.beans.AthleteLocationReceivePacket;

import java.util.ArrayList;
import java.util.List;

//import org.codehaus.jackson.JsonNode;
//import org.codehaus.jackson.JsonProcessingException;
//import org.codehaus.jackson.map.ObjectMapper;

public class ProcessXMPPMessagesFacade {

//    private ObjectMapper mapper = new ObjectMapper();
    private List<BuddyLocationUpdateHandler> buddyLocationChangedHandlers = new ArrayList<BuddyLocationUpdateHandler>();
//    private List<TextMessageComeHandler> textMessageComeHandlers = new ArrayList<TextMessageComeHandler>();

    public void registerBuddyLocationChangedHandler(BuddyLocationUpdateHandler handler) {
        buddyLocationChangedHandlers.add(handler);
    }

    public void unregisterBuddyLocationChangedHanlder(BuddyLocationUpdateHandler handler) {
        buddyLocationChangedHandlers.remove(handler);
    }

    private void fireBuddyLocationChangedEvent(AthleteLocationReceivePacket packetAthlete) {
        for (BuddyLocationUpdateHandler handler : buddyLocationChangedHandlers) {
            handler.updateBuddyLocation(packetAthlete);
        }
    }

//    public void registerTextMessageComeHanlder(TextMessageComeHandler handler) {
//        textMessageComeHandlers.add(handler);
//    }

//    public void TextMessageCome(TextMessageComeHandler handler) {
//        textMessageComeHandlers.remove(handler);
//    }

//    private void fireTextMessageComeEvent(TextMessageReceivePacket packet) {
//        for (TextMessageComeHandler handler : textMessageComeHandlers) {
//            handler.handleMessage(packet);
//        }
//    }
    private static ProcessXMPPMessagesFacade instance;

    public static ProcessXMPPMessagesFacade getInstance() {
        if (instance == null) {
            instance = new ProcessXMPPMessagesFacade();
        }
        return instance;
    }

//    public void processMessage(Message message) {
//        XMPPReceivePacket packet = parsePacket(message);
//        if (packet instanceof AthleteLocationReceivePacket) {
//            AthleteLocationReceivePacket locationReceivePacket = (AthleteLocationReceivePacket) packet;
////            makes sure that the packet received from the server is not the own user
//            if (!String.valueOf(locationReceivePacket.getSenderId()).equals(UserConfiguration.getInstance().getUserId())) {
//                fireBuddyLocationChangedEvent(locationReceivePacket);
//            }
//        }
////        else if (packet instanceof TextMessageReceivePacket) {
////            fireTextMessageComeEvent((TextMessageReceivePacket) packet);
////        }
//    }

//    public XMPPReceivePacket parsePacket(Message message) {
//        XMPPReceivePacket packet = null;
//        try {
//            String body = message.getBody();
//            if (body.endsWith("}") && body.startsWith("{")) {
//                JsonNode root = mapper.readTree(message.getBody());
//                double lat = root.get("lat").getDoubleValue();
//                double lng = root.get("lng").getDoubleValue();
//                double height = root.get("height").getDoubleValue();
//                double speed = root.get("speed").getDoubleValue();
//                String senderName = root.get("username").getTextValue();
//                int senderId = root.get("userId").getIntValue();
//                packet = new AthleteLocationReceivePacket(senderId, senderName, lat, lng, height, speed);
//            }
////            else {
////                String from = message.getFrom();
////                packet = new TextMessageReceivePacket(1, from.substring(0, from.indexOf("@klimatur.hiof.no")), body);
////            }
//        } catch (JsonProcessingException ex) {
//            Logger.getLogger(ProcessXMPPMessagesFacade.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(ProcessXMPPMessagesFacade.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return packet;
//    }
}
